package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.ModelReplace;
import com.company.project.service.ModelReplaceService;
import com.github.pagehelper.PageHelper;
import com.company.project.core.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

/**
* Created by CodeGenerator on 2018/01/02.
*/
@RestController
@RequestMapping("/model/replace")
public class ModelReplaceController {
    @Resource
    private ModelReplaceService modelReplaceService;

    @RequestMapping("/add")
    public Result add(ModelReplace modelReplace) {
    	modelReplace.setCreateTime(new Date());
        modelReplaceService.save(modelReplace);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        modelReplaceService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/update")
    public Result update(ModelReplace modelReplace) {
        modelReplaceService.update(modelReplace);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/detail")
    public Result detail(Integer id) {
        ModelReplace modelReplace = modelReplaceService.findById(id);
        return ResultGenerator.genSuccessResult(modelReplace);
    }

    
    @ResponseBody
    @RequestMapping("/list")
    public PageInfo list(HttpServletRequest request) {
    	int page = 1;
    	int limit = 10;
    	String pageStr = request.getParameter("page");
    	String limitStr = request.getParameter("limit");
    	if (StringUtils.isNotEmpty(pageStr)) {
			try {
				page = Integer.parseInt(pageStr);
			} catch (NumberFormatException e) {
				page = 1;
			}
		}
    	if (StringUtils.isNotEmpty(limitStr)) {
    		try {
    			limit = Integer.parseInt(limitStr);
    		} catch (NumberFormatException e) {
    			limit=10;
    		}
    	}
        PageHelper.startPage(page, limit);
        List<ModelReplace> list = modelReplaceService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
